package com.atguigu.boot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.AutoConfigurationPackages;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties;
import org.springframework.cache.interceptor.CacheAspectSupport;
import org.springframework.context.ConfigurableApplicationContext;

import java.util.Arrays;

/**
 * @SpringBootApplication 这是一个SpringBoot应用
 *
 * 主程序类;主配置类
 * @author : leimingchao
 * @date Date : 2021年10月30日 8:33
 */
@SpringBootApplication
public class MainApplication {

    public static void main(String[] args) {
        //1.返回我们的ioc容器
        ConfigurableApplicationContext context = SpringApplication.run(MainApplication.class, args);
        //2.查看容器里面的组件
        Arrays.stream(context.getBeanDefinitionNames()).forEach(System.out::println);
        int count = context.getBeanDefinitionCount();
        System.out.println(count);
        String[] beanNamesForType = context.getBeanNamesForType(CacheAspectSupport.class);
        System.out.println("====:" + beanNamesForType.length);
        String[] beanNamesForType1 = context.getBeanNamesForType(WebMvcProperties.class);
        System.out.println("----------->:" + beanNamesForType1.length);
        //3.从容器中获取组件,注册的组件默认是单实例的
        // Pet pet = context.getBean("tom", Pet.class);
        // Pet pet2 = context.getBean("tom", Pet.class);
        // System.out.println("组件：" + (pet == pet2));
        //
        // MyConfig myConfig = context.getBean(MyConfig.class);
        // //proxyBeanMethods = true：com.atguigu.boot.config.MyConfig$$EnhancerBySpringCGLIB$$90c0ba3d@3050ac2f
        // //proxyBeanMethods = false：com.atguigu.boot.config.MyConfig@71e4b308
        // System.out.println(myConfig);
        //
        // //如果@Configuration(proxyBeanMethods = true)代理对象调用方法。SpringBoot
        // //总会检查这个组件是否在容器中有，如果有，就不会新创建；如果没有，就创建，目的是保证组件单实例
        // User user = myConfig.user01();
        // User user1 = myConfig.user01();
        // //proxyBeanMethods = true------> true
        // //proxyBeanMethods = false------> false
        // System.out.println(user == user1);
        //
        // User user01 = context.getBean("user01", User.class);
        // Pet tom = context.getBean("tom", Pet.class);
        // System.out.println("用户的宠物:" + (user01.getPet() == tom));
        // System.out.println("*************************************");
        // //5.获取组件
        // Arrays.stream(context.getBeanNamesForType(User.class)).forEach(System.out::println);
        // DBHelper dbHelper = context.getBean(DBHelper.class);
        // System.out.println(dbHelper);

        boolean tom = context.containsBean("tom");
        System.out.println("容器中Tom组件：" + tom);

        boolean user01 = context.containsBean("user01");
        System.out.println("容器中user01组件：" + user01);

        boolean tom22 = context.containsBean("tom22");
        System.out.println("容器中tom22组件：" + tom22);

        boolean haha = context.containsBean("haha");
        boolean hehe = context.containsBean("hehe");
        System.out.println("haha:" + haha);
        System.out.println("hehe:" + hehe);
        //获取标注了@AutoConfigurationPackage的类所在的包名
        System.out.println(AutoConfigurationPackages.get(context));
    }
}
